home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6571 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  78 lines

  1. Path: druid.borland.com!usenet
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C beginner needs your help ASAP
  5. Date: 19 Feb 1996 20:46:44 GMT
  6. Organization: Borland International
  7. Distribution: world
  8. Message-ID: <4ganjk$hr1@druid.borland.com>
  9. References: <4g862f$p0b@risky.ecs.umass.edu> <4g8ahd$p8b@spectator.cris.com>
  10. NNTP-Posting-Host: pbecker.borland.com
  11. Mime-Version: 1.0
  12. Content-Type: Text/Plain; charset=ISO-8859-1
  13. X-Newsreader: WinVN 0.99.5
  14.  
  15. In article <4g8ahd$p8b@spectator.cris.com>, aubrey@concentric.net says...
  16. >
  17. >In article <4g862f$p0b@risky.ecs.umass.edu>, sebag@ecs.umass.edu says...
  18. >>
  19. >>I am a new C programmmer who desperately needs help.
  20. >>I have been digging in the manuals for a way to do this but have still
  21. >>come out empty handed. Here is the problem: I want to open files in a while
  22. >>loop with different filenames. data0,data1,data2,data3, .. data1000 , ...
  23. >>I need to increment an integer each time around then convert it to a string
  24. >>and then somehow use strcat to combine the "data" with the integer string.
  25. >>After that use fopen(filename, "a");
  26. >>
  27. >>If you know how to do this, please email me. It would take you 2 minutes but
  28. >>it is taking me two days so far. Code would be extremly useful.
  29. >>Thanks, sebag@ecs.umass.edu
  30. >
  31. >This is how I would do it. I tested it and it seems to do what you want. Of 
  32. >course, I am no expert, in fact I was called "ignorant and idiot" by Mister 
  33. Dan 
  34. >Pop, the self-appointed comp.lang.c.police, for helping someone earlier, so I 
  35. >guess I am not worthy of posting here, but somehow I feel a little better 
  36. after 
  37. >helping someone, instead of attacking and belittling them because I somehow 
  38. see 
  39. >myself as superior when I don't really know anything about them.
  40. >
  41. >#include <stdio.h>
  42. >
  43. >main()
  44. >{
  45. >        char buf[3],filename[9];
  46. >        int  i;
  47. >
  48. >        for(i=0; i<10; i++)
  49. >        {
  50. >                sprintf(buf,"%d",i);
  51. >                strcpy( filename,"data");
  52. >                strcat( filename, buf );
  53. >                strcat( filename, ".ext");
  54. >                printf( "%s\n", filename);
  55. >        }
  56. >
  57. >}
  58. >
  59.  
  60. This is basically right, but it has has one significant problem: 
  61. "data1000.ext" requires 13 chars, not 9. Once that is fixed, the code can be 
  62. made much more succinct:
  63.  
  64. #include <stdio.h>
  65.  
  66. int main()
  67. {
  68.     int i;
  69.     char filename[13];
  70.     for( i = 0; i < 10; i++ )
  71.         {
  72.         sprintf( filename, "data%d.ext", i );
  73.         printf( "%s\n", filename );
  74.         }
  75.     return 0;
  76. }
  77.  
  78.